package 螺旋矩阵;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 螺旋矩阵
 * @date 2022/10/14 18:15
 */
public class Demo56 {
    public static void main(String[] args) {
        System.out.println( generateMatrix(4));
    }
    public static int[][] generateMatrix(int n) {
     //始终遵循左闭右开原则
      int[][] nums=new int[n][n];
      int i=0;
      int j=0;
      int count=0;
      int lunci=1;
      while (lunci<=n/2){
         for (;j<n-lunci;j++){
             nums[i][j]=++count;
         }
          for (; i <n-lunci; i++) {
              nums[i][j]=++count;
          }
          for (; j>lunci-1; j--) {
              nums[i][j]=++count;
          }
          //每次到达的边界条件
          for (; i>lunci-1; i--) {
              nums[i][j]=++count;
          }
          i++;
          j++;
          lunci++;
      }
      if(n%2==1){
          nums[i][j]=++count;
      }
      return nums;
    }
}
